package com.metaverse.backend.service;

import com.metaverse.backend.config.RedisCacheKey;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 局号信息存储
 */
@AllArgsConstructor
@Slf4j
@Data
@Service
public class RoundHelper {



    private RedisTemplate<String, Object> redisTemplate;



    /**
     * 当前局号
     * @param roomId
     * @return
     */
    public String currentRoundId(String roomId){
        String key = RedisCacheKey.ROUND_ID + roomId;
        //改配置默认为空
        String roundId = Objects.toString(redisTemplate.opsForValue().get(key),"");
        if(StringUtils.isEmpty(roundId)){
            log.warn("当前房间的对局为空,房间ID:{}" ,  roomId);
        }

        return roundId;
    }


    /**
     * 设置局号
     * @param roomId
     * @param roundId
     * @return
     */
    public void setCurrentRoundId(String roomId, String roundId){
        String key = RedisCacheKey.ROUND_ID + roomId;
        redisTemplate.opsForValue().set(key, roundId, RoomDataManage.REDIS_CACHE_TIMEOUT, TimeUnit.SECONDS);
    }


    /**
     * 删除局号
     * @param roomId
     */
    public void deleteCurrentRoundId(String roomId){
        String key = RedisCacheKey.ROUND_ID + roomId;
        redisTemplate.delete(key);
    }


}
